package com.tigercel.xiangchengyun.ui.activities

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.ReportListAdapter
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil

import kotlinx.android.synthetic.main.activity_convenient_people_details.*
import kotlinx.android.synthetic.main.include_titlebar.*
import org.jetbrains.anko.toast

class ConvenientPeopleDetailsActivity : AppCompatActivity() {
    private var content_id = ""
    private var model_name = ""
    private var model = ""
    private var viewtimes = ""
    private var isHandle = false
    private var link_title = ""
    private var link_url = ""
    private var remark_time = ""
    private var listAdapter: ReportListAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_convenient_people_details)
        content_id = intent.getStringExtra("content_id")
        model_name = intent.getStringExtra("model_name")
        model = intent.getStringExtra("model")
        Log.e("model", this.model)
        Log.e("modelName", this.model_name)
        titlebar_tv.text = "报料详情"
        titlebar_iv_left.setImageDrawable(resources.getDrawable(R.drawable.btn_nav_back))
        titlebar_iv_left.visibility = View.VISIBLE
        titlebar_iv_left.setOnClickListener(View.OnClickListener { this.finish() })
        viewtimes = intent.getIntExtra("viewtimes", 0).toString()
        isHandle = intent.getBooleanExtra("isHandle", false)
        tv_handle.text = if (isHandle) "已处理" else "待处理"
        getContent(content_id, model_name)

    }

    private fun getContent(id: String, model1: String) {

        OkGo.get<String>(Global.baseUrl + Global.facilitate_info)
                .tag(this)
                .params("facilitate_id", id)
                .params("model", model1)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val result = response!!.body()
                        val status = JsonUtil.get_key_string("status", result)
                        val msg = JsonUtil.get_key_string("msg", result)
                        val code = JsonUtil.get_key_string("code", result)
                        val data = JsonUtil.get_key_string("data", result)
                        if (status == "0") {
                            val facilitate = JsonUtil.get_key_string("facilitate", data)
                            val title = JsonUtil.get_key_string("title", facilitate)
                            val content = JsonUtil.get_key_string("content", facilitate)
                            val pub_time = JsonUtil.get_key_string("pub_time", facilitate)
                            val pub_loc = JsonUtil.get_key_string("pub_loc", facilitate)
                            val remark = JsonUtil.get_key_string("remark", facilitate)
                            val pub_type = JsonUtil.get_key_string("pub_type", facilitate)
                            val link = JsonUtil.get_key_string("link", facilitate)
                            val imgs = JsonUtil.getList("imgs", facilitate)
                            tv_title.text = title
                            tv_type.text = when (model_name) {
                                "facilitate_life" -> "民生"
                                "facilitate_fm" -> "FM936"
                                else -> "其他"
                            }
                            tv_content.text = content
                            tv_lookUp.text = viewtimes
                            tv_time.text = pub_time
                            tv_location.text = pub_loc
                            if (remark.isNullOrBlank()) {
                                li_flow.visibility = View.GONE
                            } else {
                                remark_time = JsonUtil.get_key_string("remark_time", facilitate)
                                li_flow.visibility = View.VISIBLE
                                tv_gzTime.text = remark_time
                                tv_flow_title.text = remark
                            }
                            tv_pub_type.text = pub_type
                            if (link.isNullOrBlank()) {
                                li_flow_detils.visibility = View.GONE
                            } else {
                                link_title = JsonUtil.get_key_string("title", link)
                                link_url = JsonUtil.get_key_string("url", link)
                                li_flow_detils.visibility = View.VISIBLE
                                tv_flow_details.text = link_title
                            }
                            li_flow_detils.setOnClickListener(View.OnClickListener {
                                var intent = Intent()
                                intent.putExtra("title", link_title)
                                intent.putExtra("url", link_url)
                                intent.setClass(this@ConvenientPeopleDetailsActivity, WebViewActivity::class.java)
                                startActivity(intent)
                            })
                            if (listAdapter == null) {
                                listAdapter = ReportListAdapter(this@ConvenientPeopleDetailsActivity, imgs)
                                list_img.adapter = listAdapter
                            } else {
                                listAdapter!!.notifyDataSetChanged()
                            }
                        } else {
                            toast(getString(R.string.readFaile) )
                        }
                    }

                })
    }
}
